/**
  ******************************************************************************
  * @file    sh30f9xx_sa0_led.h
  * @author  sinowealth
  * @version V1.1.0
  * @date    2021-01-07  
  * @brief   This file provide LED module's APIs
  ******************************************************************************
  * @attention
  *
  * SINOWEALTH IS SUPPLYING THIS SOFTWARE FOR USE EXCLUSIVELY SH_ON SINOWEALTH'S 
  * MICROCONTROLLER PRODUCTS. IT IS PROTECTED UNDER APPLICABLE COPYRIGHT LAWS. 
  * THIS SOFTWARE IS FOR GUIDANCE IN ORDER TO SAVE TIME. AS A RESULT, SINOWEALTH 
  * SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES 
  * WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH FIRMWARE AND/OR
  * THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN 
  * CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2020 Sinowealth</center></h2>
  *
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SH30F9XX_SA0_LED_H
#define __SH30F9XX_SA0_LED_H

#ifdef __cplusplus
extern "C"
{
#endif

/* Includes ------------------------------------------------------------------*/
#include "sh30f9xx_sa0_libcfg.h"

/** @addtogroup SH30F9xx_sa0_libcfg_StdLib_Driver
  * @{
  */

/** @addtogroup LED_MODULE
  * @{
  */

#define LED_FLAG_COM         (uint16_t)0x0001
#define LED_FLAG_FRAME       (uint16_t)0x0002

#define IS_LED_FLAG(FLAG)    (((FLAG & (uint16_t)0xFFFC) == 0x00) && (FLAG != 0x00))

/** 
  * @brief  LED COMCR COMSEL mode seclet  
  */
typedef enum
{
    LED_COMCR_COM0  = 0x0000,
    LED_COMCR_COM1  = 0x0001,
    LED_COMCR_COM2  = 0x0002,
    LED_COMCR_COM3  = 0x0003,
    LED_COMCR_COM4  = 0x0004,
    LED_COMCR_COM5  = 0x0005,
    LED_COMCR_COM6  = 0x0006,
    LED_COMCR_COM7  = 0x0007,
}LEDCOMCR_COMSEL_TypeDef;

#define IS_LEDCOMCR_MODE(COMCRMODE)    (((COMCRMODE) == LED_COMCR_COM0)    \
                                       || ((COMCRMODE) == LED_COMCR_COM1)  \
                                       || ((COMCRMODE) == LED_COMCR_COM2)  \
                                       || ((COMCRMODE) == LED_COMCR_COM3)  \
                                       || ((COMCRMODE) == LED_COMCR_COM4)  \
                                       || ((COMCRMODE) == LED_COMCR_COM5)  \
                                       || ((COMCRMODE) == LED_COMCR_COM6)  \
                                       || ((COMCRMODE) == LED_COMCR_COM7))

    typedef enum
    {
        LED_ContCurnt_BitSel0 = 0x01,
        LED_ContCurnt_BitSel1 = 0x02,
        LED_ContCurnt_BitSel2 = 0x04,
        LED_ContCurnt_BitSel3 = 0x08,
        LED_ContCurnt_BitSel4 = 0x10,
        LED_ContCurnt_BitSel5 = 0x20,
        LED_ContCurnt_BitSel6 = 0x40,
        LED_ContCurnt_BitSel7 = 0x80,
    } LED_ContCurnt_BitSel_TypeDef;

#define IS_LEDCOM_MODE(ContCurnt_BitSel)    ((((ContCurnt_BitSel) & (uint8_t)0x00) == 0x00) \
                                           && ((ContCurnt_BitSel) != (uint8_t)0x00))


/** 
  * @brief  LED AUCR COM mode seclet  
  */
typedef enum
{
    LED_AUCR_COM0  = 0x0001,
    LED_AUCR_COM1  = 0x0002,
    LED_AUCR_COM2  = 0x0004,
    LED_AUCR_COM3  = 0x0008,
    LED_AUCR_COM4  = 0x0010,
    LED_AUCR_COM5  = 0x0020,
    LED_AUCR_COM6  = 0x0040,
    LED_AUCR_COM7  = 0x0080,
}LEDAUCR_COM_TypeDef;

#define IS_LEDAUCR_MODE(AUMODE)    (((AUMODE) == LED_AUCR_COM0)     \
                                   || ((AUMODE) == LED_AUCR_COM1)   \
                                   || ((AUMODE) == LED_AUCR_COM2)   \
                                   || ((AUMODE) == LED_AUCR_COM3)   \
                                   || ((AUMODE) == LED_AUCR_COM4)   \
                                   || ((AUMODE) == LED_AUCR_COM5)   \
                                   || ((AUMODE) == LED_AUCR_COM6)   \
                                   || ((AUMODE) == LED_AUCR_COM7))
/** 
  * @brief  LED SDRx value set  
  */
typedef enum
{
    LED_SDR0_SEG0  = 0x0000,
    LED_SDR0_SEG1  = 0x0001,
    LED_SDR0_SEG2  = 0x0002,
    LED_SDR0_SEG3  = 0x0003,
    LED_SDR1_SEG4  = 0x0004,
    LED_SDR1_SEG5  = 0x0005,
    LED_SDR1_SEG6  = 0x0006,
    LED_SDR1_SEG7  = 0x0007,
    LED_SDR2_SEG8  = 0x0008,
    LED_SDR2_SEG9  = 0x0009,
    LED_SDR2_SEG10  = 0x00010,
    LED_SDR2_SEG11  = 0x00011,
    LED_SDR3_SEG12  = 0x00012,
    LED_SDR3_SEG13  = 0x00013,
    LED_SDR3_SEG14  = 0x00014,
    LED_SDR3_SEG15  = 0x00015,
}LEDSDRX_SEG_TypeDef;

#define IS_LEDSDR_MODE(SDRMODE)    (((SDRMODE) == LED_SDR0_SEG0)    \
                                   || ((SDRMODE) == LED_SDR0_SEG1)  \
                                   || ((SDRMODE) == LED_SDR0_SEG2)  \
                                   || ((SDRMODE) == LED_SDR0_SEG3)  \
                                   || ((SDRMODE) == LED_SDR1_SEG4)  \
                                   || ((SDRMODE) == LED_SDR1_SEG5)  \
                                   || ((SDRMODE) == LED_SDR1_SEG6)  \
                                   || ((SDRMODE) == LED_SDR1_SEG7)  \
                                   || ((SDRMODE) == LED_SDR2_SEG8)  \
                                   || ((SDRMODE) == LED_SDR2_SEG9)  \
                                   || ((SDRMODE) == LED_SDR2_SEG10) \
                                   || ((SDRMODE) == LED_SDR2_SEG11) \
                                   || ((SDRMODE) == LED_SDR3_SEG12) \
                                   || ((SDRMODE) == LED_SDR3_SEG13) \
                                   || ((SDRMODE) == LED_SDR3_SEG14) \
                                   || ((SDRMODE) == LED_SDR3_SEG15))

/** 
  * @brief  LED COM or io seclet  
  */
typedef enum
{
    LED_COMCR_IO  = 0x0000,
    LED_COMCR_COM = 0x0001,
}LEDCOMCR_COMIO_TypeDef;


/** 
  * @brief  LED SEG width seclet  
  */
typedef enum
{
    LED_COMCR_FIX  = 0x0000,
    LED_COMCR_ALT  = 0x0001,
}LEDCOMCR_MODE_TypeDef;

/** 
  * @brief  LED AUCR AUMD seclet  
  */
typedef enum
{
    LED_AUCR_STOP  = 0x0000,
    LED_AUCR_GOON  = 0x0001,
}LEDAUCR_AUMD_TypeDef;

/** 
  * @brief  LED AUCR AUEN seclet  
  */
typedef enum
{
    LED_AUCR_SINGLE  = 0x0000,
    LED_AUCR_CONT    = 0x0001,
}LEDAUCR_AUEN_TypeDef;

/** 
  * @brief  LED CCCR (constant current source Control register) ON  
  */
    typedef enum
    {
        LED_CCCR_CCSS0 = 0x01,
        LED_CCCR_CCSS1 = 0x02,
        LED_CCCR_CCSS2 = 0x04,
        LED_CCCR_CCSS3 = 0x08,
        LED_CCCR_CCSS4 = 0x10,
        LED_CCCR_CCSS5 = 0x20,
        LED_CCCR_CCSS6 = 0x40,
        LED_CCCR_CCSS7 = 0x80,
    } LEDCCCR_TypeDef;

/*! Check the CCCR value */
#define IS_LED_CCCR(CCCRON)    ((((CCCRON) & (uint16_t)0x00) == 0x00) \
                               && ((CCCRON) != (uint16_t)0x00))

/** 
  * @brief  LED SEGMOD switch control register  
  */
typedef enum
{
    LED_SEGMOD_S0MS_SEG  = 0x0001,
    LED_SEGMOD_S1MS_SEG  = 0x0002,
    LED_SEGMOD_S2MS_SEG  = 0x0004,
    LED_SEGMOD_S3MS_SEG  = 0x0008,
    LED_SEGMOD_S4MS_SEG  = 0x0010,
    LED_SEGMOD_S5MS_SEG  = 0x0020,
    LED_SEGMOD_S6MS_SEG  = 0x0040,
    LED_SEGMOD_S7MS_SEG  = 0x0080,
    LED_SEGMOD_S8MS_SEG  = 0x0100,
    LED_SEGMOD_S9MS_SEG  = 0x0200,
    LED_SEGMOD_S10MS_SEG = 0x0400,
    LED_SEGMOD_S11MS_SEG = 0x0800,
    LED_SEGMOD_S12MS_SEG = 0x1000,
    LED_SEGMOD_S13MS_SEG = 0x2000,
    LED_SEGMOD_S14MS_SEG = 0x4000,
    LED_SEGMOD_S15MS_SEG = 0x8000,
}LEDSEGMOD_TypeDef;

/*! Check the SEGMOD value */
#define IS_LED_SEGMOD(SEGMODVAL)    ((((SEGMODVAL) & (uint16_t)0x00) == 0x00) \
                                    && ((SEGMODVAL) != (uint16_t)0x00))

/** 
  * @brief  LED CCDR (constant current source data register) ON  
  */
typedef enum
{
    LED_CCDR_8mA   = 0x00,
    LED_CCDR_10mA  = 0x01,
    LED_CCDR_12mA  = 0x02,
    LED_CCDR_14mA  = 0x03,
    LED_CCDR_16mA  = 0x04,
    LED_CCDR_18mA  = 0x05,
    LED_CCDR_20mA  = 0x06,
    LED_CCDR_22mA  = 0x07,
    LED_CCDR_24mA  = 0x08,
    LED_CCDR_26mA  = 0x09,
    LED_CCDR_28mA  = 0x0A,
    LED_CCDR_30mA  = 0x0B,
    LED_CCDR_32mA  = 0x0C,
}LEDCCDR_VALUE_TypeDef;

#define IS_LED_CCDR_VALUE(VALUESEL)    (((VALUESEL) == LED_CCDR_8mA) \
                                       || ((VALUESEL) == LED_CCDR_10mA) \
                                       || ((VALUESEL) == LED_CCDR_12mA) \
                                       || ((VALUESEL) == LED_CCDR_14mA) \
                                       || ((VALUESEL) == LED_CCDR_16mA) \
                                       || ((VALUESEL) == LED_CCDR_18mA) \
                                       || ((VALUESEL) == LED_CCDR_20mA) \
                                       || ((VALUESEL) == LED_CCDR_22mA) \
                                       || ((VALUESEL) == LED_CCDR_24mA) \
                                       || ((VALUESEL) == LED_CCDR_26mA) \
                                       || ((VALUESEL) == LED_CCDR_28mA) \
                                       || ((VALUESEL) == LED_CCDR_30mA) \
                                       || ((VALUESEL) == LED_CCDR_32mA))

/**
  * @brief define the interrupt run mode control
  * @arg   LED_INT_Continue:    continue scanf
  * @arg   LED_INT_Stop:  stop scanf
  */    
typedef enum 
{ 
    LED_INT_Continue = 0, 
    LED_INT_Stop = 1, 
}LED_INT_MODE_TypeDef; 

/**
  * @brief define scanf counter 
  * @arg   LED_SCANF_CNTSTO:      counter stop
  * @arg   LED_SCANF_CNTContinue: counter continue
  */    
typedef enum 
{ 
    LED_SCANF_CNTSTO = 0, 
    LED_SCANF_CNTContinue = 1, 
}LED_SCANF_CNTMOD_TypeDef;

/**
  * @brief define LED com Interrupt flag 
  * @arg   LED_COM_NOIF:         NO COM Interrupt flag
  * @arg   LED_COM_IF:           COM Interrupt flag
  */    
typedef enum 
{ 
    LED_COM_NOIF = 0, 
    LED_COM_IF = 1, 
}LED_COM_IF_TypeDef;

/**
  * @brief define LED frame Interrupt flag 
  * @arg   LED_FRAME_NOIF: NO COM Interrupt flag
  * @arg   LED_FRAME_IF:   COM Interrupt flag
  */    
typedef enum 
{ 
    LED_FRAME_NOIF = 0, 
    LED_FRAME_IF = 1, 
}LED_FRAME_IF_TypeDef;

/**
  * @brief CLEAR LED COM Interrupt flag 
  * @arg   LED_COM_NOIF: NO COM Interrupt flag
  * @arg   LED_COM_IF:   COM Interrupt flag
  */    
typedef enum 
{ 
    LED_COM_IF_NOAVAIL = 0, 
    LED_COM_IF_CLEAR = 1, 
}LED_COM_IFCLEAR_TypeDef;

/**
  * @brief CLEAR LED FRAME Interrupt flag 
  * @arg   LED_FRAME_NOIF: NO COM Interrupt flag
  * @arg   LED_FRAME_IF:   COM Interrupt flag
  */    
typedef enum 
{ 
    LED_FRAME_IF_NOAVAIL = 0, 
    LED_FRAME_IF_CLEAR = 1, 
}LED_FRAME_IFCLEAR_TypeDef;

/** @struct LED_SGL_InitTypeDef
  * @brief  structure for TWI initial
  */ 
typedef struct
{   /* for COMCR register */
    LEDCOMCR_COMSEL_TypeDef          LedComSEL  ;      /*!< scanf COM select at present*/
    LEDCOMCR_COMIO_TypeDef           ComOrIoSEL ;      /*!< COM or IO select*/
    LEDCOMCR_MODE_TypeDef            LedSEGWid  ;      /*!< LED SEG width select*/
    uint8_t                          LedComWidthSEL ;  /*LED COM  width select*/
}LED_SGL_InitTypeDef;

/** @struct LED_AUCR_InitTypeDef
  * @brief  structure for TWI initial
  */ 
typedef struct
{   
    /* for AUCR register */
    uint8_t                          LedAuDeadWid ;    /*led dead zone width seclet*/
    uint8_t                          LedAuComSEL   ;   /*led continr COM seclet*/
    LEDAUCR_AUMD_TypeDef             LedAuScanfCONT ;  /*led continr scanf continu or stop*/
    LEDAUCR_AUEN_TypeDef             LedAuScanfMode ;  /*led continr scanf mode seclet*/
}LED_AUCR_InitTypeDef;

 /** @addtogroup LED_Group_Pub_Funcs
  * @{
  */
/* Fills each LED_InitStruct member with its default value */
void LED_SGL_StructInit(LED_SGL_InitTypeDef *LED_InitStruct);

/* Fills each LED_InitStruct member with its default value */
void LED_AUCR_StructInit(LED_AUCR_InitTypeDef *LED_InitStruct);

/* LED driver mould Initial function */
void LED_SGL_Init(const LED_SGL_InitTypeDef *InitCfg);

/* LED driver mould Initial function */
void LED_AUCR_Init(const LED_AUCR_InitTypeDef *InitCfg);

/* Clear the LED single pending flags */
void LED_ClearFlag(uint16_t LED_Flag);

/* Clears ALL LED driver mould interrupt flag */
void LEDMOULD_ClearFlag(void);

/* Open or close the LED peripheral */
void LED_SINGLESCAN_OnOff(CmdState OnOffState);

/* Single scanf COM select now */
void LED_SglSCan_ComSel(LEDCOMCR_COMSEL_TypeDef SglComSel);
 
/* Open or close the LED AU peripheral */
void LED_AU_OnOff(CmdState OnOffState);

/* LED CCCR (constant current source Control register) ON */
void LED_ContCurnt_OnOff(uint8_t LedCCCRSet, CmdState OnOffState);

/* LED CCCR (constant current source Control register) OFF */
void LED_CCCR_ALLOFF(void);

/* Configure the LED driver mould COM port according to the specified when Continuous scanning */
void LED_ContinuScan_ComCfg(uint8_t ContinuScan_ComCfg);

/* LED CCCR (constant current source data register) select  */
void LED_ContCurnt_ValueSEL(uint8_t ContCurnt_BitSel, LEDCCDR_VALUE_TypeDef LedCCDRSel);

/* LED CCCR (constant current source data register) reset  */
void LED_CCDR_ALLRESET(void);

/* LED LED SEGMOD switch control register  */
void LED_SEGMOD_SEGSEL(uint16_t LedSEGSEL);

/* LED LED SEGMOD switch control register reset  */
void LED_SEGMODE_ALLRESET(void);

/* LED contiue scan SEG ouput controlr  */
void LED_AUSORX_COMSEL(LEDCOMCR_COMSEL_TypeDef AUSORXCOMSEL, uint16_t SEGSEL);

/* LED contiue scan SEG ouput control reset  */
void LED_AUSORX_ALLRESET(void);

/* Enable or disable the specified LED frame interrupt */
void LED_INT_AUFrameConfig(FunctionalState NewState);

/* Enable or disable the specified LED com interrupt */
void LED_INT_SGL_ComConfig(FunctionalState NewState);

/* Enable or disable the specified LED frame DMA */
void LED_DMA_AUFrameEN(FunctionalState NewState);

/* Enable or disable the specified LED com DMA */
void LED_DMA_ComEN(FunctionalState NewState);

/* Check whether the specified LED COM flag is set or not */
FlagStatus LED_GetComFlagStatus(LED_TypeDef *LEDMOUDLE);

/* Check whether the specified LED Frame flag is set or not */
FlagStatus LED_GetFrameFlagStatus(LED_TypeDef *LEDMOUDLE);

/* LED contiue scanf SEG ouput control */
void LED_SEG_WIDTH_CFG(LEDSDRX_SEG_TypeDef LEDSDRX, uint8_t SDRXSEL);

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif /*__SH30F9XX_SA0_LED_H */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT Sinowealth *****END OF FILE****/
